package webreg.model.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javax.swing.JComponent;

/** Print preview class for registration
 * 
 * @see http://www.java.de/archiv/article/articleview/117/1/16/
 */
public class RegistrationPrintPreview extends JComponent {

	private static final long serialVersionUID = 1L;
	private final static int DEFAULT_PREVIEW_SIZE = 300;
    private final static double MINIMUM_ZOOM_FACTOR = 0.3;
    
    public RegistrationPrintPreview(Pageable pageable, double zoom) {
        this.pageable = pageable;
        PageFormat format = pageable.getPageFormat(index);
        if (zoom == 0.0) {
            if (format.getOrientation() == PageFormat.PORTRAIT)
                this.zoom = DEFAULT_PREVIEW_SIZE / format.getHeight();
            else 
                this.zoom = DEFAULT_PREVIEW_SIZE / format.getWidth();
        } else
            this.zoom = zoom;
        resize();
    }
    
    protected void paintPaper(Graphics g, PageFormat format) {
        g.setColor(Color.white);
        g.fillRect(0, 0, (int)format.getWidth(), (int)format.getHeight());        
        g.setColor(Color.black);
        g.drawRect(0, 0, (int)format.getWidth() - 1, (int)format.getHeight() - 1);        
    }
    
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.scale(zoom, zoom);
        try {
            PageFormat format = pageable.getPageFormat(index);   
            Printable printable = pageable.getPrintable(index);
            paintPaper(g, format);
            printable.print(g, format, 0);
        } catch (PrinterException e) {
        } catch (IndexOutOfBoundsException e) {
        }
    }

    public void moveIndex(int indexStep) {
        int newIndex = index + indexStep;
        try {
            //Printable printable = pageable.getPrintable(newIndex);
            resize();
            index = newIndex;
        } catch (IndexOutOfBoundsException ignored) {
        }
    }
    
    public void changeZoom(double zoom) {
        this.zoom = Math.max(MINIMUM_ZOOM_FACTOR, this.zoom + zoom);
        resize();
    }
    
    public void resize() {
        PageFormat format = pageable.getPageFormat(index);
        int size = (int)Math.max(format.getWidth() * zoom, format.getHeight() * zoom);
        setPreferredSize(new Dimension(size, size));
        revalidate();
    }
    
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }
    
    protected Pageable pageable;
    protected int index = 0;
    protected double zoom = 0.0;
}
